#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_COPYRIGHT((c) the Regents of the University of California)
AC_REVISION($Revision$)
AC_PREREQ(2.59)

AC_INIT([FastBit], [ibis1.3.8], [fastbit-users@hpcrdm.lbl.gov])
AC_CONFIG_MACRO_DIR([tests/m4])
AC_CONFIG_SRCDIR([src/index.cpp])
AC_CONFIG_HEADER([src/fastbit-config.h])
AC_DEFINE(PACKAGE_URL, "http://sdm.lbl.gov/fastbit/", [FastBit web site])
AM_INIT_AUTOMAKE([no-define])
AM_MAINTAINER_MODE([disable])

LIB_VERSION_INFO=0:9:0
# Checks for programs.
#AC_PROG_YACC
#AC_PROG_LEX
AC_PROG_CC
AC_PROG_CXX
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_LIBTOOL
LT_INIT
AC_LANG(C++)

# Checks for libraries.
AC_CHECK_LIB([m], [log10])
AC_CHECK_LIB([rt], [clock_gettime])
###AC_CHECK_LIB([pthread], [pthread_self], , AC_MSG_ERROR(FastBit requires pthread support))

# Checks for header files.
#AC_HEADER_STDC
AC_HEADER_DIRENT
AC_CHECK_HEADERS([inttypes.h stdint.h sys/types.h])
# string.h strings.h sys/time.h float.h limits.h stdlib.h fcntl.h unistd.h

# Checks for typedefs, structures, and compiler characteristics.
#AC_HEADER_STDBOOL
#AC_C_CONST
#AC_C_INLINE
#AC_C_VOLATILE
#AC_TYPE_OFF_T
#AC_TYPE_SIZE_T
#AC_STRUCT_TM
#AC_HEADER_TIME
#AC_TYPE_INT16_T
#AC_TYPE_INT32_T
#AC_TYPE_INT64_T
#AC_TYPE_UINT64_T
#AC_TYPE_UINT32_T
#AC_TYPE_UINT16_T

# Checks for library functions.
#AC_FUNC_MALLOC
#AC_FUNC_MEMCMP
#AC_FUNC_MMAP
#AC_FUNC_REALLOC
#AC_FUNC_SELECT_ARGTYPES
#AC_FUNC_STRTOD
AC_FUNC_VPRINTF
#AC_CHECK_FUNCS([getpagesize getpass gettimeofday memset mkdir] , ,)
AC_CHECK_FUNCS([gettimeofday mkdir] , ,)

# ----- stuff not generated by autoscan
# rename some automatically defined variables
#AC_DEFINE_UNQUOTED(FASTBIT_BUGREPORT, ${PACKAGE_BUGREPORT}, [Define to the address where bug reports for this package should be sent.])
#AC_DEFINE_UNQUOTED(FASTBIT_VERSION, ${PACKAGE_VERSION}, [Define to the version of this package.])
#AC_DEFINE_UNQUOTED(FASTBIT_STRING, ${PACKAGE_STRING}, [Define to the full name and version of this package.])
#AC_DEFINE_UNQUOTED(FASTBIT_NAME, ${PACKAGE_NAME}, [Define to the full name of this package.])

AC_ARG_ENABLE(static-pic,
	[AS_HELP_STRING([--enable-static-pic],[add '-static -fPIC' for some systems such as compute node linux (default: no)])],
	[if test "$enableval" = "yes" ; then
	CFLAGS="${CFLAGS} -static -fPIC"
	CXXFLAGS="${CXXFLAGS} -static -fPIC"
	fi])

AC_MSG_CHECKING(whether to enable debugging)
AC_ARG_ENABLE(debug,
              [AS_HELP_STRING([--enable-debug],[turn on debugging (default: set by autoconf)])],
	      [if test "$enableval" = "yes" ; then debug="yes" ; else debug="no" ; fi],[debug=""])
AC_MSG_RESULT($debug)
AC_MSG_CHECKING(whether to enable extra optimization)
AC_ARG_ENABLE(xopt,
	      [AS_HELP_STRING([--enable-xopt],[turn on extra optimization (default: no, leave the default CXXFLAGS alone)])],
	      [if test "$enableval" = "yes" ; then xopt="yes" ; else xopt="no" ; fi],[xopt="no"])
AC_MSG_RESULT($xopt)
if test "x$debug" = "xyes" ; then
  if test "x$xopt" = "xno" ; then
    if test `echo $CFLAGS | fgrep -- '-O' | wc -l` -gt 0 ; then
      OIFS=$IFS
      IFS=' '
      newflags=""
      for g in $CFLAGS ; do if test "x$g" != "x-O" -a "x$g" != "x-O2" -a "x$g" != "x-O3" ; then newflags="$newflags $g"; fi; done
      CFLAGS=$newflags
      IFS=$OIFS
    fi
    if test `echo $CXXFLAGS | fgrep -- '-O' | wc -l` -gt 0 ; then
      OIFS=$IFS
      IFS=' '
      newflags=""
      for g in $CXXFLAGS ; do if test "x$g" != "x-O" -a "x$g" != "x-O2" -a "x$g" != "x-O3" ; then newflags="$newflags $g"; fi; done
      CXXFLAGS=$newflags
      IFS=$OIFS
    fi
  fi
  if test `echo $CFLAGS | fgrep -- '-g' | wc -l` -eq 0 ; then
    CFLAGS="$CFLAGS -g"
  fi
  if test `echo $CXXFLAGS | fgrep -- '-g' | wc -l` -eq 0 ; then
    CXXFLAGS="$CXXFLAGS -g"
  fi
elif test "x$debug" = "xno" ; then
  if test `echo $CFLAGS | fgrep -- '-g' | wc -l` -gt 0 ; then
    OIFS=$IFS
    IFS=' '
    newflags=""
    for g in $CFLAGS ; do if test "x$g" != "x-g" -a "x$g" != "x-g1" -a "x$g" != "x-g2" -a "x$g" != "x-g3" ; then newflags="$newflags $g"; fi; done
    CFLAGS=$newflags
    IFS=$OIFS
  fi
  if test `echo $CXXFLAGS | fgrep -- '-g' | wc -l` -gt 0 ; then
    OIFS=$IFS
    IFS=' '
    newflags=""
    for g in $CXXFLAGS ; do if test "x$g" != "x-g" -a "x$g" != "x-g1" -a "x$g" != "x-g2" -a "x$g" != "x-g3" ; then newflags="$newflags $g"; fi; done
    CXXFLAGS=$newflags
    IFS=$OIFS
  fi
fi
if test "x$xopt" = "xyes" ; then
   # add -O5 if the compiler accepts it, otherwise -O3
   OLDCXXFLAGS=$CXXFLAGS;
   CXXFLAGS="$CXXFLAGS -O5";
   AC_COMPILE_IFELSE([
      AC_LANG_PROGRAM([[
	#include <cmath>
	]], [[
	return (1 == static_cast<int>(std::sqrt(2.0)));
   	]])], [], [CXXFLAGS="$OLDCXXFLAGS -O3"])
fi

AC_ARG_WITH(getpwuid,
	AS_HELP_STRING([--with-getpwuid],[specify --without-getpwuid to disable the attempt to use getpwuid to determine the user name]),
	[if test "x$with_getpwuid" != "xno" ; then
	AC_DEFINE(HAVE_GETPWUID, 1, [Set to 1 based on --with-getpwuid])
	fi],
	[AC_MSG_CHECKING([for the presence of getpwuid])
AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
#include<sys/types.h>
#include <pwd.h>]],
[[uid_t id = getuid();
struct passwd* pw = getpwuid(id);
if (pw == 0) return -1;]])],
[AC_DEFINE(HAVE_GETPWUID, 1, [Set to 1 if getpwuid compiles and runs correctly])
AC_MSG_RESULT(yes)],
[AC_MSG_RESULT(no)])
AC_MSG_CHECKING([for the presence of getpwuid_r])
AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
#include<sys/types.h>
#include <pwd.h>]],
[[uid_t id = getuid();
struct passwd pw;
struct passwd *ptr;
size_t nbuf = sysconf(_SC_GETPW_R_SIZE_MAX);
char *buf = new char[nbuf];
if (getpwuid_r(id, &pw, buf, nbuf, &ptr) == 0) return 0;
else return -1;]])],
[AC_DEFINE(HAVE_GETPWUID_R, 1, [Set to 1 if getpwuid_r compiles and runs correctly])
AC_MSG_RESULT(yes)],
[AC_MSG_RESULT(no)])])

AC_ARG_ENABLE(quiet-nan,
	AS_HELP_STRING([--enable-quiet-nan], [attempt use std::numeric_limits<T>::quiet_NaN() as the null value, if not possible, use std::nueric_limits<T>::max() instead]),
	[if test "$enableval" = "no"; then try_quiet_nan=0;
	else try_quiet_nan=1; fi],
	[try_quiet_nan=2])
AC_MSG_CHECKING([for quiet_NaN])
if test ${try_quiet_nan} -ne 0 ; then
  AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <stdlib.h>
#include <limits>]],
   [[try {
     double tmp = std::numeric_limits<double>::quiet_NaN();
     tmp *= 1.0 + rand();
     return(tmp == tmp);
     }
     catch (...) {
       return -1;
     }]])],
     [AC_MSG_RESULT([yes, use quiet_NaN() as null values])
       AC_DEFINE(FASTBIT_FLOAT_NULL, std::numeric_limits<float>::quiet_NaN(), [define the null values used])
       AC_DEFINE(FASTBIT_DOUBLE_NULL, std::numeric_limits<double>::quiet_NaN(), [define the null values used])],
     [AC_MSG_RESULT([no, use max() as null values])
       AC_DEFINE(FASTBIT_FLOAT_NULL, std::numeric_limits<float>::max(), [define the null values used])
       AC_DEFINE(FASTBIT_DOUBLE_NULL, std::numeric_limits<double>::max(), [define the null values used])])
else
  AC_MSG_RESULT([use max() as null values based the user supplied option])
  AC_DEFINE(FASTBIT_FLOAT_NULL, std::numeric_limits<float>::max(), [define the null values used])
  AC_DEFINE(FASTBIT_DOUBLE_NULL, std::numeric_limits<double>::max(), [define the null values used])
fi

AC_ARG_WITH(pthread,
	AS_HELP_STRING([--with-pthread=dir],[use dir as pthread root directory]),
        [if test -x $with_pthread/pthread-config; then
	       CFLAGS="$CFLAGS `$with_pthread/pthread-config --cflags"
	       CXXFLAGS="$CXXFLAGS `$with_pthread/pthread-config --cflags"
	       LIBS="$LIBS `pthread-config --ldflags` `pthread-config --libs`"
	 elif test -d $with_pthread; then
 	       if test -f $with_pthread/pthread.h; then
	         CFLAGS="$CFLAGS -I$with_pthread";
	         CPPFLAGS="$CPPFLAGS -I$with_pthread";
	         CXXCPP="$CXXCPP -I$with_pthread";
	         CXXFLAGS="$CXXFLAGS -I$with_pthread";
	       elif test -f $with_pthread/include/pthread.h; then
	         CFLAGS="$CFLAGS -I$with_pthread/include";
	         CPPFLAGS="$CPPFLAGS -I$with_pthread/include";
	         CXXCPP="$CXXCPP -I$with_pthread/include";
	         CXXFLAGS="$CXXFLAGS -I$with_pthread/include";
	       else
	         AC_MSG_WARN([Can not find $with_pthread/pthread.h or $with_pthread/include/pthread.h])
	       fi;
	       if test -f $with_pthread/libpthread.a; then
	         LIBS="$LIBS $with_pthread/libpthread.a"
	       elif test -f $with_pthread/libpthreadGC2.a; then
	         LIBS="$LIBS $with_pthread/libpthreadGC2.a"
	       elif test -f $with_pthread/lib/libpthread.a; then
	         LIBS="$LIBS $with_pthread/lib/libpthread.a"
	       elif test -f $with_pthread/lib/libpthreadGC2.a; then
	         LIBS="$LIBS $with_pthread/lib/libpthreadGC2.a"
	 else
	         AC_MSG_WARN([Can not find $with_pthread/libpthread.a, $with_pthread/libpthreadGC2.a, $with_pthread/lib/libpthread.a, or $with_pthread/lib/libpthreadGC2.a])
	 fi;
fi])

# function to check for pthread library
AC_DEFUN([Z_CHECK_PTHREAD], [
AC_REQUIRE([AC_CANONICAL_HOST])
AC_LANG_PUSH(C++)
AC_LANG([C])

z_check_pthread_ok=no
# check if the user has set any of the PTHREAD_LIBS, etcetera
# environment variables, and if threads linking works using them
if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
   save_CFLAGS="$CFLAGS"
   CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
   save_LIBS="$LIBS"
   LIBS="$PTHREAD_LIBS $LIBS"
   AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
   AC_TRY_LINK_FUNC(pthread_join, z_check_pthread_ok=yes)
   AC_MSG_RESULT($z_check_pthread_ok)
   if test x"$z_check_pthread_ok" = xno; then
       PTHREAD_LIBS=""
       PTHREAD_CFLAGS=""
   fi
   LIBS="$save_LIBS"
   CFLAGS="$save_CFLAGS"
fi

z_check_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
# pthreads: AIX (must check this before -lpthread)
# none: in case threads are in libc; should be tried before -Kthread and
#       other compiler flags to prevent continual compiler warnings
# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
# -pthreads: Solaris/gcc
# -mthreads: Mingw32/gcc, Lynx/gcc
# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
#      doesn't hurt to check since this sometimes defines pthreads too;
#      also defines -D_REENTRANT)
#      ... -mt is also the pthreads flag for HP/aCC
# pthread: Linux, etcetera
# --thread-safe: KAI C++
# pthread-config: use pthread-config program (for GNU Pth library)

case "${host_cpu}-${host_os}" in
        *solaris*)
        # On Solaris, look for -pthreads and -lpthread first:
        z_check_pthread_flags="-pthreads pthread -mt -pthread $z_check_pthread_flags"
        ;;
esac

if test x"$z_check_pthread_ok" = xno; then
for flag in $z_check_pthread_flags; do

    case $flag in
        none)
        AC_MSG_CHECKING([whether pthreads work without any flags])
        ;;

        -*)
        AC_MSG_CHECKING([whether pthreads work with $flag])
        PTHREAD_CFLAGS="$flag"
        ;;

        pthread-config)
        AC_CHECK_PROG(z_check_pthread_config, pthread-config, yes, no)
        if test x"$z_check_pthread_config" = xno; then continue; fi
        PTHREAD_CFLAGS="`pthread-config --cflags`"
        PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
        ;;

        *)
        AC_MSG_CHECKING([for the pthreads library -l$flag])
        PTHREAD_LIBS="-l$flag"
        ;;
    esac

    save_LIBS="$LIBS"
    save_CFLAGS="$CFLAGS"
    LIBS="$PTHREAD_LIBS $LIBS"
    CFLAGS="$CFLAGS $PTHREAD_CFLAGS"

    # Check for pthread_join because it is in -lpthread on IRIX
    # while pthread_create is in libc.  Check for pthread_attr_init
    # due to DEC craziness with -lpthreads.  Check for
    # pthread_cleanup_push because it is one of the few pthread
    # functions on Solaris that doesn't have a non-functional libc stub.
    AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]], [[pthread_t th; pthread_join(th, 0);
                 pthread_attr_init(0); pthread_cleanup_push(0, 0);
                 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ]])],[z_check_pthread_ok=yes],[])

    LIBS="$save_LIBS"
    CFLAGS="$save_CFLAGS"

    AC_MSG_RESULT($z_check_pthread_ok)
    if test "x$z_check_pthread_ok" = xyes; then
        break;
    fi

    PTHREAD_LIBS=""
    PTHREAD_CFLAGS=""
done
fi

# Various other checks:
if test "x$z_check_pthread_ok" = xyes; then
    LIBS="$PTHREAD_LIBS $LIBS"
    save_CFLAGS="$CFLAGS"
    CFLAGS="$CFLAGS $PTHREAD_CFLAGS"

    AC_MSG_CHECKING([if more special flags are required for pthreads])
    flag=no
    case "${host_cpu}-${host_os}" in
        *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
        *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
    esac
    AC_MSG_RESULT(${flag})
    if test "x$flag" != xno; then
        PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
    fi

    CFLAGS="$save_CFLAGS"
fi

AC_SUBST(PTHREAD_CFLAGS)
# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
if test x"$z_check_pthread_ok" = xyes; then
        ifelse([$1],,AC_DEFINE(HAVE_PTHREAD, 1, [Define if you have POSIX threads libraries and header files.]),[$1])
        :
else
        z_check_pthread_ok=no
        $2
fi
AC_LANG_POP([])
])
#end AC_DEFUN Z_CHECK_PTHREAD
Z_CHECK_PTHREAD(, [AC_MSG_ERROR(FastBit requires pthread support)])

AC_MSG_CHECKING([if math.h defines nextafter])
AC_TRY_LINK([
  #include <math.h>
],[
  double pi=3.141592654;
  return (nextafter(pi, 4.0) > pi) ? 0 : -1;
],[
  AC_MSG_RESULT([yes])
  AC_DEFINE(HAVE_NEXTAFTER, 1, [Define to 1 if nextafter is defined in math.h and is in libm.])
],[
  AC_MSG_RESULT([no])
])

AC_LINK_IFELSE(
	[AC_LANG_PROGRAM([[#include <stdlib.h>]],
	 [[char *s=0; long long tmp = strtoll("1", &s, 0);]])],
	[AC_DEFINE(HAVE_STRTOLL, 1, [Set to 1 if strtoll is defined in stdlib.h])],
	[])

# Does this compiler have built-in functions for atomic memory access?
AC_MSG_CHECKING([if compiler supports __sync_add_and_fetch for 32-bit integers])
AC_TRY_LINK([
  #include <stdint.h>
],[
  int32_t variable=5;
  return (__sync_add_and_fetch(&variable, 3)
         && __sync_sub_and_fetch(&variable, 3)) ? 0 : -1;
],[
  AC_MSG_RESULT([yes])
  AC_DEFINE(HAVE_GCC_ATOMIC32, 1, [Define to 1 if support of __sync_add_and_fetch() on 32-bit integers])
],[
  # Retry with -march set for x86.
  old_CFLAGS="$CFLAGS"
  CFLAGS="$CFLAGS -march=i686"
  AC_TRY_LINK([
    #include <stdint.h>
  ],[
    int32_t variable=3;
    return (__sync_add_and_fetch(&variable, 2)
           && __sync_sub_and_fetch(&variable, 2)) ? 0 : -1;
  ],[
    AC_MSG_RESULT([yes])
    AC_DEFINE(HAVE_GCC_ATOMIC32, 1, [Define to 1 if support __sync_add_and_fetch() on 32-bit integers])
  ],[
    AC_MSG_RESULT([no])
    CFLAGS="$old_CFLAGS"
  ])
])
AC_MSG_CHECKING([if compiler supports __sync_add_and_fetch for 64-bit integers])
AC_TRY_LINK([
  #include <stdint.h>
],[
  int64_t variable=7;
  return (__sync_add_and_fetch(&variable, 4)
         && __sync_sub_and_fetch(&variable, 4)) ? 0 : -1;
],[
  AC_MSG_RESULT([yes])
  AC_DEFINE(HAVE_GCC_ATOMIC64, 1, [Define to 1 if support __sync_add_and_fetch() on 64-bit integers])
],[
  # Retry with -march set for x86.
  old_CFLAGS="$CFLAGS"
  CFLAGS="$CFLAGS -march=i686"
  AC_TRY_LINK([
    #include <stdint.h>
    ],[
    int64_t variable=6;
    return (__sync_add_and_fetch(&variable, 2)
           && __sync_sub_and_fetch(&variable, 2)) ? 0 : -1;
  ],[
    AC_MSG_RESULT([yes])
    AC_DEFINE(HAVE_GCC_ATOMIC64, 1, [Define to 1 if support __sync_add_and_fetch() on 64-bit integers])
  ],[
    AC_MSG_RESULT([no])
    CFLAGS="$old_CFLAGS"
  ])
])
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"

# make sure FlexLexer.h defines yywrap
#AC_CHECK_HEADER(FlexLexer.h, [], [CFLAGS="${CFLAGS} -I ${PWD}/win" CXXFLAGS="${CXXFLAGS} -I ${PWD}/win"])
AC_MSG_CHECKING([if the default FlexLexer.h defines yywrap])
if AC_TRY_COMMAND($CXX $CXXFLAGS -c src/whereLexer.cc) && test -s whereLexer.o; then
  AC_MSG_RESULT([yes])
else
  T=`which flex`
  if test ${#T} -gt 9 ; then
    FLDIR="-I `echo $T | cut -c 1-$((${#T} - 8))`include"
    if AC_TRY_COMMAND($CXX $CXXFLAGS $FLDIR -c src/whereLexer.cc) && test -s whereLexer.o; then
      AC_MSG_RESULT([yes, use ${FLDIR}])
      AM_CPPFLAGS="${AM_CPPFLAGS} ${FLDIR}"
    else
      if AC_TRY_COMMAND($CXX $CXXFLAGS -I ${PWD}/win -D WITHOUT_FASTBIT_CONFIG_H -c src/whereLexer.cc) && test -s whereLexer.o; then
        AC_MSG_RESULT([no, use ${PWD}/win/FlexLexer.h])
	AM_CPPFLAGS="${AM_CPPFLAGS} -I ${PWD}/win"
      else
        AC_MSG_ERROR([no, must have FlexLexer.h to continue])
      fi
    fi
  else
    if AC_TRY_COMMAND($CXX $CXXFLAGS -I ${PWD}/win -D WITHOUT_FASTBIT_CONFIG_H -c src/whereLexer.cc) && test -s whereLexer.o; then
      AC_MSG_RESULT([no, use ${PWD}/win/FlexLexer.h])
      AM_CPPFLAGS="${AM_CPPFLAGS} -I ${PWD}/win"
    else
      AC_MSG_ERROR([no, must have FlexLexer.h to continue])
    fi
  fi
fi
rm -f whereLexer.o


# check for java include directory
AC_DEFUN([AC_JNI_INCLUDE_DIR],[
JNI_INCLUDE_DIRS=""
dnl case "$host_os" in
dnl   darwin*) _JTOPDIR=`echo "$JAVA_HOME" | sed -e 's:/[[^/]]*$::'`
dnl            _JINC="$_JTOPDIR/Headers";;
dnl   *)       _JINC="$JAVA_HOME/include";;
dnl esac
if test -f "$JAVA_HOME/include/jni.h"; then
  JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $JAVA_HOME/include"
elif test -f "$JAVA_HOME/Headers/jni.h"; then
  JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $JAVA_HOME/Headers"
else
  AC_MSG_WARN([cannot find jni.h in $JAVA_HOME/Headers or $JAVA_HOME/include, try one directory up])
  JAVA_HOME=`echo "$JAVA_HOME" | sed -e 's:/[[^/]]*$::'`
  if test -f "$JAVA_HOME/include/jni.h"; then
    JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $JAVA_HOME/include"
  elif test -f "$JAVA_HOME/Headers/jni.h"; then
    JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $JAVA_HOME/Headers"
  else
    AC_MSG_WARN([cannot find jni.h in $JAVA_HOME/Headers or $JAVA_HOME/include])
    JNI_INCLUDE_DIRS=""
  fi
fi

# system specific java includes
case "$host_os" in
  bsdi*)     _JNI_INC_SUBDIRS="bsdos";;
  linux*)    _JNI_INC_SUBDIRS="linux genunix";;
  osf*)      _JNI_INC_SUBDIRS="alpha";;
  solaris*)  _JNI_INC_SUBDIRS="solaris";;
  mingw*)    _JNI_INC_SUBDIRS="win32";;
  cygwin*)   _JNI_INC_SUBDIRS="win32";;
  *)         _JNI_INC_SUBDIRS="genunix";;
esac

# add all subdirectories
for JINCSUBDIR in $_JNI_INC_SUBDIRS
do
  if test -d "$JAVA_HOME/include/$JINCSUBDIR"; then
    JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $JAVA_HOME/include/$JINCSUBDIR"
  fi
done
])
# does command java work
AC_DEFUN([AC_PROG_JAVA_WORKS], [
AC_CHECK_PROG(uudecode, uudecode$EXEEXT, yes)
if test "x$uudecode" = xyes; then
AC_CACHE_CHECK([if uudecode can decode base 64 file], ac_cv_prog_uudecode_base64, [
dnl /**
dnl  * Test.java: used to test if java compiler works.
dnl  */
dnl public class Test
dnl {
dnl
dnl public static void
dnl main( String[] argv )
dnl {
dnl     System.exit (0);
dnl }
dnl
dnl }
cat << \EOF > Test.uue
begin-base64 644 Test.class
yv66vgADAC0AFQcAAgEABFRlc3QHAAQBABBqYXZhL2xhbmcvT2JqZWN0AQAE
bWFpbgEAFihbTGphdmEvbGFuZy9TdHJpbmc7KVYBAARDb2RlAQAPTGluZU51
bWJlclRhYmxlDAAKAAsBAARleGl0AQAEKEkpVgoADQAJBwAOAQAQamF2YS9s
YW5nL1N5c3RlbQEABjxpbml0PgEAAygpVgwADwAQCgADABEBAApTb3VyY2VG
aWxlAQAJVGVzdC5qYXZhACEAAQADAAAAAAACAAkABQAGAAEABwAAACEAAQAB
AAAABQO4AAyxAAAAAQAIAAAACgACAAAACgAEAAsAAQAPABAAAQAHAAAAIQAB
AAEAAAAFKrcAErEAAAABAAgAAAAKAAIAAAAEAAQABAABABMAAAACABQ=
====
EOF
if uudecode$EXEEXT Test.uue; then
        ac_cv_prog_uudecode_base64=yes
else
        echo "configure: __oline__: uudecode had trouble decoding base 64 file 'Test.uue'" >&AS_MESSAGE_LOG_FD
        echo "configure: failed file was:" >&AS_MESSAGE_LOG_FD
        cat Test.uue >&AS_MESSAGE_LOG_FD
        ac_cv_prog_uudecode_base64=no
fi
rm -f Test.uue])
fi
if test "x$ac_cv_prog_uudecode_base64" != xyes; then
        rm -f Test.class
        #AC_MSG_WARN([I have to compile Test.class from scratch])
        if test "x$ac_cv_prog_javac_works" = xno; then
                AC_MSG_WARN([Cannot compile java source. $JAVAC does not work properly])
        fi
        if test "x$ac_cv_prog_javac_works" = x; then
                AC_PROG_JAVAC
        fi
fi
AC_CACHE_CHECK(if $JAVA works, ac_cv_prog_java_works, [
JAVA_TEST=Test.java
CLASS_TEST=Test.class
TEST=Test
changequote(, )dnl
cat << \EOF > $JAVA_TEST
/* [#]line __oline__ "configure" */
public class Test {
public static void main (String args[]) {
        System.exit (0);
} }
EOF
changequote([, ])dnl
if test "x$ac_cv_prog_uudecode_base64" != xyes; then
        if AC_TRY_COMMAND($JAVAC $JAVACFLAGS $JAVA_TEST) && test -s $CLASS_TEST; then
                :
        else
          echo "configure: failed program was:" >&AS_MESSAGE_LOG_FD
          cat $JAVA_TEST >&AS_MESSAGE_LOG_FD
          AC_MSG_WARN(The Java compiler $JAVAC failed (see config.log, check the CLASSPATH?))
	  JAVAC="no" # reset to empty string
        fi
fi
if AC_TRY_COMMAND($JAVA $JAVAFLAGS $TEST) >/dev/null 2>&1; then
  ac_cv_prog_java_works=yes
else
  echo "configure: failed program was:" >&AS_MESSAGE_LOG_FD
  cat $JAVA_TEST >&AS_MESSAGE_LOG_FD
  AC_MSG_WARN(The Java VM $JAVA failed (see config.log, check the CLASSPATH?))
  JAVA="no"
fi
rm -fr $JAVA_TEST $CLASS_TEST Test.uue
])
AC_PROVIDE([$0])dnl
]
)
# JAVAC
AC_DEFUN([AC_PROG_JAVAC_WORKS],[
	AC_CACHE_CHECK([if $JAVAC works], ac_cv_prog_javac_works, [
	JAVA_TEST=Test.java
	CLASS_TEST=Test.class
cat << \EOF > $JAVA_TEST
/* [#]line __oline__ "configure" */
public class Test {
}
EOF
if AC_TRY_COMMAND($JAVAC $JAVACFLAGS $JAVA_TEST) >/dev/null 2>&1; then
  ac_cv_prog_javac_works=yes
else
  AC_MSG_ERROR([The Java compiler $JAVAC failed (see config.log, check the CLASSPATH?)])
  echo "configure: failed program was:" >&AS_MESSAGE_LOG_FD
  cat $JAVA_TEST >&AS_MESSAGE_LOG_FD
fi
rm -f $JAVA_TEST $CLASS_TEST
])
AC_PROVIDE([$0])dnl
])
AC_DEFUN([AC_PROG_JAVAC],[
	AC_REQUIRE([AC_EXEEXT])dnl
	if test "x$JAVAPREFIX" = x; then
        test "x$JAVAC"=x && AC_CHECK_PROGS(JAVAC, "gcj$EXEEXT -C" guavac$EXEEXT jikes$EXEEXT javac$EXEEXT)
	else
        #test "x$JAVAC" = x && AC_CHECK_PROGS(JAVAC, "gcj$EXEEXT -C" guavac$EXEEXT jikes$EXEEXT javac$EXEEXT, $JAVAPREFIX/javac$EXEEXT)
	JAVAC=$JAVAPREFIX/javac$EXEEXT
	fi
	test "x$JAVAC" = x && AC_MSG_ERROR([no acceptable Java compiler found in \$PATH])
	AC_PROG_JAVAC_WORKS
	AC_PROVIDE([$0])dnl
	])
AC_DEFUN([AC_PROG_JAVA],[
	AC_REQUIRE([AC_EXEEXT])dnl
	if test "x$JAVAPREFIX" = x; then
        test "x$JAVA" = x && AC_CHECK_PROGS(JAVA, kaffe$EXEEXT java$EXEEXT)
	else
        #test "x$JAVA" = x && AC_CHECK_PROGS(JAVA, kaffe$EXEEXT java$EXEEXT, $JAVAPREFIX/java$EXEEXT)
	JAVA=$JAVAPREFIX/java$EXEEXT
	fi
	test x$JAVA = x && AC_MSG_ERROR([no acceptable Java virtual machine found in \$PATH])
	AC_PROG_JAVA_WORKS
	AC_PROVIDE([$0])dnl
	])

# Set the optional Java SDK directory.
AC_ARG_WITH(java,
	    AS_HELP_STRING([--with-java=dir],[use dir as Java SDK root directory]),
            [if test -d $with_java -a -f $with_java/bin/javac$EXEEXT; then JAVA_HOME=$with_java; fi])
#AC_MSG_RESULT([with_java=$with_java])
if test "x$with_java" != "xyes" ; then
  if test "x$with_java" = "xno" ; then
    JAVA_HOME="" # don't use JAVA
  elif ! test -d "$JAVA_HOME" -a -f "$JAVA_HOME/bin/javac$EXEEXT" ; then
    AC_MSG_WARN([Can not find $JAVA_HOME/bin/javac$EXEEXT])
    JAVA_HOME="" # don't use JAVA
  fi
elif test "x$JAVA_HOME" = "x" ; then # guess where is JDK
  if test "x$JAVA_PATH" != "x" ; then
     if test -x "$JAVA_PATH"/javac$EXEEXT ; then
       JAVA_HOME=`basename "$JAVA_PATH"`
     elif test -x "$JAVA_PATH"/bin/javac$EXEEXT ; then
       JAVA_HOME="$JAVA_PATH"
     fi
  elif test -x /usr/libexec/java_home ; then
    JAVA_HOME=`/usr/libexec/java_home`
  else
    case "${host_os}" in
    # On Darwin the JDK, if present, is installed in /Library/Java/Home .
    darwin*)
      JAVA_HOME="/Library/Java/Home"
      ;;
    # On Fedora Core systems the JDK is usually in /usr/java/default , others use /usr
    linux*)
      if test -x "/usr/java/default/bin/javac" ; then
        JAVA_HOME="/usr/java/default"
      elif test -x "/usr/bin/javac" ; then
        JAVA_HOME="/usr"
      else # don't use java
        JAVA_HOME=""
      fi
      ;;
    *)
    ;;
    esac
  fi
fi

#AC_MSG_RESULT([JAVA_HOME=$JAVA_HOME])
if test "x$JAVA_HOME" != "x" -a -d "$JAVA_HOME"
then
  JAVAPREFIX="${JAVA_HOME}/bin"
  # Check for Java programs.  Order here is important: check first
  # for the compiler.
  AC_PROG_JAVAC
  AC_PROG_JAVA

  if test ! "x$JAVAC" = "xno"
  then
    AC_JNI_INCLUDE_DIR
    for JNI_INCLUDE_DIR in $JNI_INCLUDE_DIRS
    do
      CFLAGS="$CFLAGS -I$JNI_INCLUDE_DIR"
      CPPFLAGS="$CPPFLAGS -I$JNI_INCLUDE_DIR"
      CXXCPP="$CXXCPP -I$JNI_INCLUDE_DIR"
      CXXFLAGS="$CXXFLAGS -I$JNI_INCLUDE_DIR"
    done

    AC_CHECK_HEADER(jni.h, [HAS_JNI=yes], [HAS_JNI=no])
  fi
fi

# build java test program if java and javac exist
AM_CONDITIONAL(BUILD_JAVA_TEST, test ! "x$JAVA" = "xno" &&  test ! "x$ac_cv_javac_supports_enums" = "xno")
AM_CONDITIONAL(BUILD_JAVA_INTERFACE, test ! "x$JAVAC" = "xno" &&  test "x$HAS_JNI" = "xyes")
AC_MSG_CHECKING([whether to build java native interface])
if test ! "x$JAVAC" = "xno" &&  test "x$HAS_JNI" = "xyes"; then
AC_MSG_RESULT([yes])
else
AC_MSG_RESULT([no])
fi

AC_MSG_CHECKING(whether to configure Makefiles in contrib)
AC_ARG_ENABLE(contrib,
              [AS_HELP_STRING([--enable-contrib],[build contributed code in contrib directory (default: no)])],
	      [if test "$enableval" = "yes" ; then contrib="yes" ; else contrib="no" ; fi],[contrib="no"])
AC_MSG_RESULT(${contrib})
AM_CONDITIONAL(BUILD_CONTRIB, test "$contrib" = "yes")
AC_SUBST(BUILD_CONTRIB)

AC_DEFINE_UNQUOTED(FASTBIT_IBIS_INT_VERSION,
 `echo $PACKAGE_VERSION | awk 'BEGIN { FS="." } { printf("%d%.2d%.2d%.2d\n", substr($1, 5), $2, $3, $4); }'`,
[Define an integer version of FastBit IBIS version number])
OBJDIR=$objdir
AC_SUBST(BUILD_JAVA_INTERFACE)
AC_SUBST(LIB_VERSION_INFO)
AC_SUBST(PTHREAD_CFLAGS)
AC_SUBST(PTHREAD_LIBS)
AC_SUBST(AM_CPPFLAGS)
AC_SUBST(CXXFLAGS)
AC_SUBST(CFLAGS)
AC_SUBST(OBJDIR)
#end stuff not generated by autoscan

AC_CONFIG_FILES([Makefile src/Makefile examples/Makefile tests/Makefile
	         java/Makefile contrib/Makefile contrib/fbmerge/Makefile])
AC_CONFIG_FILES([tests/scripts/fastbit-config],
		[chmod a+x tests/scripts/fastbit-config])
AC_CONFIG_COMMANDS([FastBit-prefix],
[rm -f src/stamp-h1;
mv src/fastbit-config.h src/stamp-h1;
sed -e 's/ PACKAGE_/ FASTBIT_/' src/stamp-h1 > src/fastbit-config.h;
if test ! src/fastbit-config.h -nt src/stamp-h1; then sleep 1; touch src/fastbit-config.h; else :; fi
])
#
AC_OUTPUT
